<template>
  <div>
    <!-- 面包屑视图 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>首页</el-breadcrumb-item>
      <el-breadcrumb-item>角色管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 添加角色按钮区域 -->
    <el-row>
      <el-button type="primary">添加按钮</el-button>
      <!-- 角色列表区域 -->
    </el-row>
    <!-- 索引列 -->
    <el-table :data="rolerlist" border stripe>
      <el-table-column type="expand">
        <template v-slot="scope">
          <!-- 渲染一级权限 -->
          <el-row
            :class="['dbbottom', i1 ? 'dbtop' : '', 'ventet']"
            v-for="(item1, i1) in scope.row.children"
            :key="item1.id"
          >
            <el-col :span="5">
              <el-tag closable @close="getrolerclose(scope.row, item1.id)">
                {{ item1.authName }}
                <i class="el-icon-caret-right"></i>
              </el-tag>
            </el-col>
            <!-- 渲染二级和三级权限 -->
            <el-col :span="19">
              <el-row
                :class="[i2 === 0 ? '' : 'dbtop', 'ventet']"
                v-for="(item2, i2) in item1.children"
                :key="item2.id"
              >
                <el-col :span="6">
                  <el-tag
                    type="success"
                    closable
                    @close="getrolerclose(scope.row, item2.id)"
                  >
                    {{ item2.authName }}
                    <i class="el-icon-caret-right"></i>
                  </el-tag>
                </el-col>
                <el-col :span="18">
                  <el-tag
                    type="warning"
                    :class="[i3 === 0 ? '' : 'dbtop']"
                    v-for="(item3, i3) in item2.children"
                    :key="item3.id"
                    closable
                    @close="getrolerclose(scope.row, item3.id)"
                    >{{ item3.authName }}
                  </el-tag>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </template>
      </el-table-column>
      <el-table-column type="index" label="#"></el-table-column>
      <el-table-column prop="roleName" label="角色名称"></el-table-column>
      <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
      <el-table-column label="操作" width="300px">
        <template v-slot="scope">
          <!-- 修改 -->
          <el-button type="primary" icon="el-icon-edit" size="mini">
            编辑
          </el-button>
          <!-- 删除 -->
          <el-button type="danger" icon="el-icon-delete" size="mini">
            删除
          </el-button>
          <!-- 分配角色 -->
          <el-button
            type="warning"
            icon="el-icon-setting"
            size="mini"
            @click="showedialogVisible(scope.row)"
          >
            分配权限
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      title="提示"
      :visible.sync="treeVisible"
      width="50%"
      @close="getrigtclose"
    >
      <el-tree
        :data="rightlist"
        :props="treeProps"
        show-checkbox
        node-key="id"
        default-expand-all
        :default-checked-keys="defaults"
        ref="treeref"
      ></el-tree>
      <span slot="footer">
        <el-button @click="treeVisible = false">取 消</el-button>
        <el-button type="primary" @click="qtreeget">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 所有的角色列表数据
      rolerlist: [],
      // 获取所有的权限数据
      rightlist: [],
      // 显示权限对话框的显示与隐藏
      treeVisible: false,
      treeProps: {
        children: 'children',
        label: 'authName'
      },
      defaults: [],
      // 当前即将分配的id
      roleid: ''
    }
  },
  created() {
    this.getrolerlist()
  },
  // 获取权限列表
  methods: {
    async getrolerlist() {
      const { data: res } = await this.$http.get('roles')
      if (res.meta.status !== 200) {
        return this.$message.error('获取角色列表成功')
      }
      this.rolerlist = res.data
      //   console.log(res)
    },
    // 删除权限列表
    async getrolerclose(role, rightId) {
      const conf = await this.$confirm(
        '此操作将永久删除该数据, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      if (conf !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      const { data: res } = await this.$http.delete(
        `roles/${role.id}/rights/${rightId}`
      )
      if (res.meta.status !== 200) {
        return this.$message.error('删除失败')
      }
      this.$message.success('删除成功')
      // this.getrolerlist()
      role.children = res.data
    },
    async showedialogVisible(role) {
      this.roleid = role.id
      const { data: res } = await this.$http.get('rights/tree')
      if (res.meta.status !== 200) {
        this.$message.error('获取树状数据失败')
      }
      this.rightlist = res.data
      // console.log(this.rightlist)
      this.getdefaults(role, this.defaults)
      // console.log(this.getdefaults)

      this.treeVisible = true
    },
    // 通过递归的形式，获取角色下所有三级权限id，并保存到defaults里面
    getdefaults(node, arr) {
      // 如果当前node节点不包含children属性，则是三级节点
      if (!node.children) {
        return arr.push(node.id)
      }

      node.children.forEach(item => this.getdefaults(item, arr))
    },
    getrigtclose() {
      this.defaults = []
    },
    async qtreeget() {
      const kesw = [
        ...this.$refs.treeref.getCheckedKeys(),
        ...this.$refs.treeref.getHalfCheckedNodes()
      ]
      const idstr = kesw.join(',')
      const { data: res } = await this.$http.post(
        `roles/${this.roleid}/rights`,
        { rids: idstr }
      )
      if (res.meta.status !== 200) {
        return this.$message.error('分配权限失败')
      }
      this.getrolerlist()
      this.treeVisible = false
    }
  }
}
</script>
<style lang="less" scoped>
.el-tag {
  margin: 10px;
}
.dbtop {
  border-top: 1px solid #eee;
}
.dbbottom {
  border-bottom: 1px solid #eee;
}
.ventet {
  display: flex;
  align-items: center;
}
</style>
